from typing import List, Dict

from motor.core import AgnosticCollection

from common_bind_service.common.dao.base_model_dao import BaseModelDao
from common_bind_service.models.container import ContainerStatus
from common_bind_service.utils.db import get_container_status


class ContainerStatusDao(BaseModelDao[ContainerStatus]):

    def get_db(self) -> AgnosticCollection:
        return get_container_status()

    async def get_one_container_mark(self, container_mark: str) -> ContainerStatus:
        """根据配方编码获取对应的参数列表"""
        query = {'container_mark': container_mark}
        return await self.get_one_by_query(query)

    async def get_container_dict_by_mark_list(self, product_mark_list: List[str]) -> Dict[str, List[str]]:
        """根据给定的单件二维码获取它们"""
        query = {
            'product_list.mark': {'$in': product_mark_list}
        }
        db = self.get_db()
        res = {}  # 容器码, 对应的码值
        product_mark_set = set(product_mark_list)
        async for item in db.find(query, {'product_list': 1, '_id': 0, 'container_mark': 1}):
            res[item['container_mark']] = []
            for sub_item in item['product_list']:
                if sub_item['mark'] in product_mark_set:
                    res[item['container_mark']].append(sub_item['mark'])

        return res


